﻿using System.Windows;
using System.Windows.Controls;
using Common.Client.Lib.Controls;
using Syncfusion.Lib.Extensions;

namespace Syncfusion.Lib.Controls
{
    /// <summary>
    /// 用于作为SfDockingManager-Content的基类
    /// </summary>
    [TemplatePart(Name = ScrollViewer, Type = typeof(ScrollViewer))]
    public class SfDockingManagerContentControlBase : DockingManagerContentControlBase
    {

        private const string ScrollViewer = "PART_ScrollViewer";

        /// <summary>
        /// 默认构造
        /// </summary>
        public SfDockingManagerContentControlBase()
        {
            SfDockingManagerExtension.SetSfDockingManagerDependencyProperty(this);
            //在DockingManager中，不建议设置宽度及高度；指定宽度或高度，可能导致滚动条行为异常
            this.SetResourceReference(FrameworkElement.StyleProperty, "SfDockingManagerContentControlBaseStyle");
        }


        /// <summary>
        /// 设置上级ScrollViewer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnSetParentScrollViewer(object sender, RoutedEventArgs e)
        {
            if (GetTemplateChild(ScrollViewer) is ScrollViewer parentScrollViewer)
            {
                if (parentScrollViewer.HorizontalScrollBarVisibility != ParentScrollViewerHorizontalScrollBarVisibility)
                {
                    parentScrollViewer.HorizontalScrollBarVisibility = ParentScrollViewerHorizontalScrollBarVisibility;
                }

                if (parentScrollViewer.VerticalScrollBarVisibility != ParentScrollViewerVerticalScrollBarVisibility)
                {
                    parentScrollViewer.VerticalScrollBarVisibility = ParentScrollViewerVerticalScrollBarVisibility;
                }

                if (ParentScrollViewerVerticalScrollBarVisibility != ScrollBarVisibility.Auto
                    && ParentScrollViewerHorizontalScrollBarVisibility != ScrollBarVisibility.Auto
                    && IsSetParentScrollViewerSizeChangedEvent == false)
                {
                    this.SizeChanged += ContentControlBase_SizeChanged;
                    IsSetParentScrollViewerSizeChangedEvent = true;
                }
            }
        }

        /// <summary>
        /// 处理窗体大小改变，导致滚动条计算异常的情况
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void ContentControlBase_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (GetTemplateChild(ScrollViewer) is ScrollViewer parentScrollViewer)
            {
                if (IsSetParentScrollViewerSizeChangedEvent)
                {
                    if (parentScrollViewer.HorizontalScrollBarVisibility !=
                        ParentScrollViewerHorizontalScrollBarVisibility)
                    {
                        parentScrollViewer.HorizontalScrollBarVisibility =
                            ParentScrollViewerHorizontalScrollBarVisibility;
                    }

                    if (parentScrollViewer.VerticalScrollBarVisibility != ParentScrollViewerVerticalScrollBarVisibility)
                    {
                        parentScrollViewer.VerticalScrollBarVisibility = ParentScrollViewerVerticalScrollBarVisibility;
                    }
                }

                e.Handled = true;
            }
        }
    }
}
